# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=phonon
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"
)
pkgver=4.12.0
pkgrel=3
pkgdesc="The multimedia framework by KDE (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: phonon-qt5'
)
msys2_repository_url='https://invent.kde.org/libraries/phonon/'
url='https://community.kde.org/Phonon'
license=('spdx:LGPL-2.1-or-later')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-qt5-base"
  "${MINGW_PACKAGE_PREFIX}-qt5-tools"
  "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
  "${MINGW_PACKAGE_PREFIX}-qt6-base"
  "${MINGW_PACKAGE_PREFIX}-qt6-tools"
)
source=("https://download.kde.org/stable/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz"{,.sig})
sha256sums=('3287ffe0fbcc2d4aa1363f9e15747302d0b080090fe76e5f211d809ecb43f39a'
            'SKIP')
validpgpkeys=('E0A3EB202F8E57528E13E72FD7574483BB57B18D') # Jonathan Esk-Riddell <jr@jriddell.org>

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local _kde_f5_KDE_INSTALL_DIRS=(
    -DKDE_INSTALL_LIBDIR=lib
    -DKDE_INSTALL_QMLDIR=share/qt5/qml
    -DKDE_INSTALL_QTPLUGINDIR=share/qt5/plugins
    -DKDE_INSTALL_DBUSDIR=share/dbus-1
    -DKDE_INSTALL_MANDIR=share/man
    -DKDE_INSTALL_APPDIR=share/applications
    -DKDE_INSTALL_MIMEDIR=share/mime/packages
    -DECM_MKSPECS_INSTALL_DIR="${MINGW_PREFIX}"/share/qt5/mkspecs/modules
  )

  # phononsettings.exe conflicts with phonon-qt6
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DECM_MKSPECS_INSTALL_DIR=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_kde_f5_KDE_INSTALL_DIRS[@]}" \
      -DPHONON_BUILD_QT5=ON \
      -DPHONON_BUILD_QT6=OFF \
      -DPHONON_BUILD_SETTINGS=OFF \
      "${_extra_config[@]}" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-qt5"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-qt5"

  local _KDE_INSTALL_DIRS=(
    -DKDE_INSTALL_LIBDIR=lib
    -DKDE_INSTALL_QMLDIR=share/qt6/qml
    -DKDE_INSTALL_QTPLUGINDIR=share/qt6/plugins
    -DKDE_INSTALL_DBUSDIR=share/dbus-1
    -DKDE_INSTALL_MANDIR=share/man
    -DKDE_INSTALL_APPDIR=share/applications
    -DKDE_INSTALL_MIMEDIR=share/mime/packages
  )

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_KDE_INSTALL_DIRS[@]}" \
      -DPHONON_BUILD_QT5=OFF \
      -DPHONON_BUILD_QT6=ON \
      "${_extra_config[@]}" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-qt6"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}-qt6"
}

package_phonon-qt5() {
  pkgdesc+=" (Qt5)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-qt5-base"
  )
  optdepends+=(
    "${MINGW_PACKAGE_PREFIX}-qt5-tools: Designer plugin"
  )

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-qt5"

  # conflicts with phonon-qt6
  rm -r "${pkgdir}${MINGW_PREFIX}"/bin/data
}

package_phonon-qt6() {
  pkgdesc+=" (Qt6)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
    "${MINGW_PACKAGE_PREFIX}-qt6-base"
  )
  optdepends+=(
    "${MINGW_PACKAGE_PREFIX}-qt6-tools: Designer plugin"
  )

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}-qt6"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
